<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function goods_list(){
        $cat=new \Admin\Model\CatModel;
        $cat_list=$cat->select();
        //如果栏目下有子栏目先把子栏目取出来，再把子栏目下的商品取出来
        $cat_id=I('get.cat_id')+0;
        $cat_wtree=$cat->getCatTree($cat_list,$id=$cat_id,$lev=0);
        $sub =array($cat_id);
        if (!empty($cat_wtree)) {//没有子孙栏目
            foreach ($cat_wtree as $v) {
                $sub[]=$v['cat_id']+0;
            }
        }
        $in=implode(',',$sub);
        $cat_ftree=$cat->getFamily($cat_list,$cat_id);
        //取出商品
        $goods=new \Think\Model();
        $goods_list=$goods->query("select * from goods where cat_id in($in)");
//        show_bug($goods_list);
        $history=array_reverse(session('history') , true);
        $cart = \Home\Tool\CartTool::getIns();
        $calcCnt=$cart->calcCnt();
        $this->assign('calcCnt',$calcCnt);
        $this->assign('history',$history);
        $this->assign('goods_list',$goods_list);
        $this->assign('cat_wtree',$cat_wtree);
        $this->assign('cat_ftree',$cat_ftree);
        $this->display();
}
    public function goods_one(){
        $goods_id=I('get.goods_id')+0;
        $good=new \Admin\Model\GoodsModel();
        $good_list=$good->where('goods_id='.$goods_id)->select();
//        show_bug($good_list);
        $his=D('Goods');
        $his->history($good_list[0]);//加入浏览历史
        $history=array_reverse(session('history') , true);
        $his->find($goods_id);
        $comment = $his->relationGet('comment'); // 查出多条相关的评价.
        /*
        注: 自动关联查询评价时,自动用goods表的主键来做连接.
        上例中,goods表的主键是goods_id,则关联时产生的sql如下:
        select * from comment where goods_id = 23;
        如果评价内容比较多,建议直接用comment的Model操作,并指定查询字段.
        一对一时,可以用关联模型简化操作.
        */
        $this->assign('comment',$comment);
        $this->assign('goods_id',$goods_id);
        $this->assign('history',$history);
        $this->assign('good_list',$good_list);
        $this->display();
    }

    public function addComment(){
        $comment=D('Comment');
        $user_name=session('user_name');
        $_POST['user_name']=$user_name;
        $_POST['pubtime']=date('Y-m-d h:i:s',time());
        $comment->create();
        $com=$comment->add($_POST);
        if (!$com){
            $this->error('商品评论添加失败');
        }
        $this->success('商品评论添加成功');
    }
}